/*
 *                  BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 * 
 * Created on Oct 11, 2007
 * 
 */

package org.biojava.services.das.registry;

import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;

import javax.xml.namespace.QName;


import org.apache.axis.Constants;
import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.encoding.Serializer;
import org.apache.axis.encoding.SerializerFactory;
import org.apache.axis.wsdl.fromJava.Types;
import org.w3c.dom.Element;
import org.xml.sax.Attributes;

public class PropertiesSerializerFactory implements 
SerializerFactory{

	private  Vector mechanisms;

	public Serializer getSerializerAs(String arg0) {
		// TODO Auto-generated method stub
		return new PropertiesSerializer();
	}

	public Iterator getSupportedMechanismTypes() {
		if  (mechanisms == null) {
			mechanisms = new  Vector();
			mechanisms.add(Constants.AXIS_SAX);
		}
		return  mechanisms.iterator();
	}



}


class PropertiesSerializer implements Serializer{

	public void serialize(QName name, Attributes attributes, Object arg2, SerializationContext context) throws IOException {
		System.out.println("serializing " + name);
		if (! (arg2 instanceof Properties)){
			System.out.println("not a properties object , but a " + arg2.getClass());
			return;
		} 
		Properties  data = (Properties) arg2;

		Set<Object> keys = data.keySet();
		for (Object k: keys){
			if ( k instanceof String) {
				String key = (String) k;
				String value = data.getProperty(key);
				context.startElement(name,  attributes);
				context.serialize(new  QName("", "key"), null, key);
				context.serialize(new  QName("", "value"), null, value);
				context.endElement();
			}
		}
	}

	public Element writeSchema(Class arg0, Types arg1) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public String getMechanismType() {
		// TODO Auto-generated method stub
		return null;
	}

}